package com.bootdo.wxnotify;

import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.lvyou.domain.LvyouMember;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyOrderAll;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyOrderContent;
import com.front.web.lvyouXcxWeb.LvyouSupperController;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class MyWxObserverImp extends WxEventObserver {

    @Override
    public void notify(Element rootElement) {
//        String openId = rootElement.elementText("FromUserName");
//        String eventKey = rootElement.elementText("EventKey");
//        if(StringUtils.isEmpty(eventKey)){
//            return;
//        }
//
//        System.err.println(
//                rootElement.elementText("Event")
//                + "》》》》 eventKey:="
//                        + eventKey.replace("qrscene_", "") +"" +
//                        "》》》》 openId:=" + openId);
//
//        Map map = new HashMap();
//        eventKey =  eventKey.replace("qrscene_", "");
//        map.put("orderNo",eventKey.replace("qrscene_", ""));
//        XinfangOrder  xinfangOrder =   xinfangOrderDao.getByOrderNo(eventKey);
//        if(xinfangOrder == null){ return;}
//        xinfangOrder.setUserOpenId(openId);
//        /**/
//        saveUserInfo(xinfangOrder);
    }

    @Override
    public void notify(LvyouSpecialtyOrderContent xinfangOrder) {

    }



    void saveUserInfo(LvyouSpecialtyOrderContent xinfangOrder){

//        XinfangVisitsUser xinfangVisitsUser =  xinfangVisitsUserService.getByIdCard(xinfangOrder.getUserIdCard());
//        if(null == xinfangVisitsUser){
//            xinfangVisitsUser = new XinfangVisitsUser();
//            xinfangVisitsUser.setName(xinfangOrder.getUserName());
//            xinfangVisitsUser.setMobile(xinfangOrder.getUserMobile());
//            xinfangVisitsUser.setAddress(xinfangOrder.getUserAddress());
//            xinfangVisitsUser.setBirthday(xinfangOrder.getUserBirthday());
//            xinfangVisitsUser.setIdCard(xinfangOrder.getUserIdCard());
//            xinfangVisitsUser.setAge(xinfangOrder.getUserAge());
//            xinfangVisitsUser.setSex(xinfangOrder.getUserSex());
//            xinfangVisitsUser.setCityId(xinfangOrder.getCityId());
//            xinfangVisitsUser.setCityName(xinfangOrder.getCityName());
//            xinfangVisitsUser.setOpenId(xinfangOrder.getUserOpenId());
//            xinfangVisitsUser.setMobile(xinfangOrder.getUserMobile());
//            xinfangVisitsUserService.save(xinfangVisitsUser);
//        }else if (StringUtils.isEmpty(xinfangVisitsUser.getOpenId())){
//            xinfangVisitsUser.setOpenId(xinfangOrder.getUserOpenId());
//            xinfangVisitsUser.setMobile(xinfangOrder.getUserMobile());
//            xinfangVisitsUserService.save(xinfangVisitsUser);
//        }
//        try {
//            XinfangWorkMember workMember  =   ShiroUtils.getUser();
//            if(null != workMember){
//                //更新案件动态
//                updateOrderLogFunction(xinfangOrder,workMember);
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }

    }

    void  updateOrderLogFunction(LvyouSpecialtyOrderContent newOrder, LvyouMember workMember){
//        XinfangOrder  oldOrder  =   xinfangOrderDao.getByOrderNo(newOrder.getOrderNo());
//
//        if(null != oldOrder){
//                /*||(oldOrder.getStatus() == null && newOrder.getStatus() == null)
//                    ||(oldOrder.getStatus() == null && newOrder.getStatus().equals(0))*/
//            if((null != oldOrder.getStatus() &&  !oldOrder.getStatus().equals(newOrder.getStatus()))
//            ){
//                updateStatusOrderLog(newOrder, oldOrder,workMember);
//            }
//            /*if(!oldOrder.getAcceptCompanyId().equals(newOrder.getAcceptCompanyId())){
//              fowordOrderLog(newOrder,oldOrder, workMember);
//            } */
//        }else{
//            XinfangOrderLog xinfangOrderLog = new XinfangOrderLog();
//            xinfangOrderLog.setOrderNo(newOrder.getOrderNo());
//            xinfangOrderLog.setContent("已在"+newOrder.getCompanyName()+"创建记录");
//            xinfangOrderLog.setType(1);
//            xinfangOrderLog.setVisitUserIdcard(newOrder.getUserIdCard());
//            xinfangOrderLog.setAcceptCompanyId(workMember.getCompanyId());
//            xinfangOrderLog.setAcceptCompanyName(workMember.getCompanyName());
//            xinfangOrderLog.setAcceptWorkMemberId(workMember.getId());
//            xinfangOrderLog.setAcceptWorkMemberName(workMember.getName());
//            xinfangOrderLog.setAcceptWorkMemberPositionName(workMember.getPositionName());
//            xinfangOrderLogService.save(xinfangOrderLog);
//            if(newOrder.getId() != null){
//                xinfangOrderDao.update(newOrder);
//            }else {
//                xinfangOrderDao.save(newOrder);
//            }
//            SendMbMessageUtils.sendUserTempMessage(newOrder,xinfangOrderLog);
//
//            if (newOrder.getStatus() == OrderStatusEmum.YZP.getType()) {
//                xinfangOrderLogPush.pushOrserLog(newOrder.getAcceptCompanyId(), "您有案件需要处理了");
//            }
//        }
    }
    void updateStatusOrderLog(LvyouSpecialtyOrderContent newOrder,LvyouSpecialtyOrderContent oldOrder,LvyouMember workMember){
//        XinfangOrderLog xinfangOrderLog = new XinfangOrderLog();
//        if(workMember.getType().intValue() == WorkMemberRoleEmum.LEA.getType()){
//            //领导批示
//            xinfangOrderLog.setType(2);
//        }else if(workMember.getType().intValue() == WorkMemberRoleEmum.EMP.getType()){
//            //普通
//            xinfangOrderLog.setType(1);
//        }else if(workMember.getType().intValue() == WorkMemberRoleEmum.REC.getType()){
//            //普通
//            xinfangOrderLog.setType(1);
//        }
//        xinfangOrderLog.setOrderNo(newOrder.getOrderNo());
//        xinfangOrderLog.setVisitUserIdcard(newOrder.getUserIdCard());
//
//        /**
//         * -1代表已结案，0代表未指派（只保存），1代表已指派（未受理），2代表已受理， 3代表被驳回, 4代表不予处理
//        */
//        switch (newOrder.getStatus().intValue()){
//            case -1:
//                xinfangOrderLog.setContent(workMember.getCompanyName()+": 已完结该案件");
//
//                xinfangOrderLogPush.pushOrserLog(oldOrder.getCompanyId(), workMember.getCompanyName() + "已完结该案件");
//                break;
//            case 0:
//                xinfangOrderLog.setContent(workMember.getCompanyName()+"：已接受处理");
//                break;
//            case 1:
//                xinfangOrderLog.setContent(/*oldOrder.getAcceptCompanyName()+*/"转由"+newOrder.getAcceptCompanyName()+"处理");
//
//                xinfangOrderLogPush.pushOrserLog(newOrder.getAcceptCompanyId(), "您有案件需要处理了");
//                break;
//            case 2:
//                //xinfangOrderLog.setContent(workMember.getCompanyName()+"已受理案件");
//                xinfangOrderLog.setContent(newOrder.getAcceptCompanyName()+"已受理案件");
//                xinfangOrderLogPush.pushOrserLog(oldOrder.getCompanyId(), newOrder.getAcceptCompanyName()+"已受理案件");
//                break;
//            case 3:
//                xinfangOrderLog.setContent(oldOrder.getAcceptCompanyName()+"已驳回案件");
//
//                xinfangOrderLogPush.pushOrserLog(oldOrder.getCompanyId(), oldOrder.getAcceptCompanyName()+"已驳回案件");
//                break;
//            case 4:
//                xinfangOrderLog.setContent(oldOrder.getCompanyName()+"不予受理");
//                break;
//        }
//        xinfangOrderLog.setAcceptCompanyId(workMember.getCompanyId());
//        xinfangOrderLog.setAcceptCompanyName(workMember.getCompanyName());
//        xinfangOrderLog.setAcceptWorkMemberId(workMember.getId());
//        xinfangOrderLog.setAcceptWorkMemberName(workMember.getName());
//        xinfangOrderLog.setAcceptWorkMemberPositionName(workMember.getPositionName());
//        xinfangOrderLogService.save(xinfangOrderLog);
//        if(newOrder.getId() != null){
//            xinfangOrderDao.update(newOrder);
//        }else {
//            xinfangOrderDao.save(newOrder);
//        }
//        SendMbMessageUtils.sendUserTempMessage(newOrder,xinfangOrderLog);
    }
    /**
     * 订单转派记录
     * @param newOrder
     * @param workMember
     */
    void fowordOrderLog(LvyouSpecialtyOrderContent newOrder, LvyouSpecialtyOrderContent oldOrder, LvyouMember workMember){
//        XinfangOrderLog xinfangOrderLog = new XinfangOrderLog();
//        if(workMember.getType().intValue() == WorkMemberRoleEmum.LEA.getType()){
//            //领导批示
//            xinfangOrderLog.setType(2);
//        }else if(workMember.getType().intValue() == WorkMemberRoleEmum.EMP.getType()){
//            //普通
//            xinfangOrderLog.setType(1);
//        }else if(workMember.getType().intValue() == WorkMemberRoleEmum.REC.getType()){
//            //普通
//            xinfangOrderLog.setType(1);
//        }
//        if(newOrder.getStatus().intValue() == OrderStatusEmum.WZP.getType()){
//            return;
//        }
//        xinfangOrderLog.setOrderNo(newOrder.getOrderNo());
//        if(StringUtils.isNotEmpty(oldOrder.getAcceptCompanyName()) && StringUtils.isEmpty(newOrder.getAcceptCompanyName())){
//            xinfangOrderLog.setContent(oldOrder.getAcceptCompanyName()+"已驳回");
//        }else if(StringUtils.isEmpty(oldOrder.getAcceptCompanyName()) && StringUtils.isNotEmpty(newOrder.getAcceptCompanyName())){
//            xinfangOrderLog.setContent("已指派"+newOrder.getAcceptCompanyName()+"处理");
//        }else{
//            xinfangOrderLog.setContent(oldOrder.getAcceptCompanyName()+"转由"+newOrder.getAcceptCompanyName()+"处理");
//        }
//        xinfangOrderLog.setAcceptCompanyId(workMember.getCompanyId());
//        xinfangOrderLog.setAcceptCompanyName(workMember.getCompanyName());
//        xinfangOrderLog.setAcceptWorkMemberId(workMember.getId());
//        xinfangOrderLog.setAcceptWorkMemberName(workMember.getName());
//        xinfangOrderLog.setAcceptWorkMemberPositionName(workMember.getPositionName());
//        xinfangOrderLogService.save(xinfangOrderLog);
//        SendMbMessageUtils.sendUserTempMessage(newOrder,xinfangOrderLog);
    }




}
